package jcu.ict.profaid;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public class MainResult extends TabActivity {
	
	private String i;
	private String questionID;
	private Menu optionsMenu;
	private String user_id;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_result);
				
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		ColorDrawable colorDrawable = new ColorDrawable(
				Color.parseColor("#0099CC"));
		actionBar.setBackgroundDrawable(colorDrawable);

		Bundle bundle = this.getIntent().getExtras();
		questionID = bundle.getString("setQuestionId");
		i = (String) bundle.getCharSequence("setInfoCourse");
		user_id = (String) bundle.getCharSequence("setUserId");

		Resources res = getResources();
		
		Intent iPercentage = new Intent(this,FormResultPercentage.class);
		bundle.putCharSequence("setQuestionId", questionID);
		bundle.putCharSequence("setCourseId", i);
		bundle.putCharSequence("setUserId", user_id);
		iPercentage.putExtras(bundle);
		
		Intent iTable = new Intent(this, FormResultTable.class);
		bundle.putCharSequence("setQuestionId", questionID);
		bundle.putCharSequence("setCourseId", i);
		bundle.putCharSequence("setUserId", user_id);
		iTable.putExtras(bundle);
				
		Intent iChart = new Intent(this, FormResultChart.class);
		bundle.putCharSequence("setQuestionId", questionID);
		bundle.putCharSequence("setCourseId", i);
		bundle.putCharSequence("setUserId", user_id);
		iChart.putExtras(bundle);
		
		TabHost mTabHst = getTabHost();

		mTabHst.addTab(mTabHst.newTabSpec("tab_test1")
				.setIndicator("Percentage", res.getDrawable(R.drawable.one))
				.setContent(iPercentage));
		
		mTabHst.addTab(mTabHst.newTabSpec("tab_test2")
				.setIndicator("Table", res.getDrawable(R.drawable.two))
				.setContent(iTable));
		
		mTabHst.addTab(mTabHst.newTabSpec("tab_test3")
				.setIndicator("Chart", res.getDrawable(R.drawable.three))
				.setContent(iChart));
		
		mTabHst.setCurrentTab(0);

	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(this).inflate(R.menu.main, menu);
		
		EditText phoneBook = null;
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			View v = menu.findItem(R.id.phoneBook).getActionView();

			if (v != null) {
				phoneBook = (EditText) v.findViewById(R.id.phoneBook);
			}
		}

		if (phoneBook != null) {
			// add.setOnEditorActionListener(onSearch);
		}
		return (super.onCreateOptionsMenu(menu));
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	    	Intent in = new Intent(MainResult.this,FormListQuestion.class);
			Bundle bundle = new Bundle();
			bundle.putString("setInfoCourse", i);
			bundle.putCharSequence("setUserId", user_id);
            in.putExtras(bundle);
			startActivity(in);
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// app icon in action bar clicked; go home
			Intent intent = new Intent(MainResult.this,MainQuestion.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			Bundle bundle = new Bundle();
			bundle.putString("setInfoCourse", i);
			bundle.putCharSequence("setUserId", user_id);
			intent.putExtras(bundle);
			startActivity(intent);
			finish();
			return true;
		 case R.id.phoneBook:
             // app icon in action bar clicked; go home
             try{
			 Intent adds = new Intent(this, FormPhoneBook.class);
             adds.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
             Bundle bundles = new Bundle();
             bundles.putCharSequence("setIdCourse", i);
             bundles.putCharSequence("setQuestionId", questionID);
             bundles.putCharSequence("setUserId", user_id);
             adds.putExtras(bundles);
             startActivity(adds);
             finish();
             }catch (Exception e) {
				Toast.makeText(getBaseContext(), "No number has been added", Toast.LENGTH_LONG).show();
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
}
